/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.action;

import java.io.IOException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaResponse;
import uber.netbsd.community.dao.comment.CommentBuilder;
import uber.netbsd.community.dao.comment.CommentManager;

/**
 * Action to add new comment
 * @author alex
 */
@UrlBinding("/action/AddComment")
public class AddCommentActionBean extends BaseActionBean {

    /**
     * Message body
     */
    @Validate(required = true, minlength = 3, maxlength = 2048)
    private String message;
    /**
     * unique page code
     */
    @Validate(required = true, minlength = 3, maxlength = 25)
    private String code;
    /**
     * where to jump after operation
     */
    @Validate(required = true)
    private String backUrl;

    public String getBackUrl() {
        return backUrl;
    }

    public void setBackUrl(String backUrl) {
        this.backUrl = backUrl;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @DefaultHandler
    public Resolution save() throws IOException {
        CommentManager cm = CommentManager.getInstance();
        CommentBuilder builder = CommentBuilder.getInstance();


        /**
         * check capcha only for anonymous users
         */
        if (getCurrentUser() == null) {

            // create recaptcha without <noscript> tags
            ReCaptcha captcha = ReCaptchaFactory.newReCaptcha("6LeJdb4SAAAAAPxK7fX5sz4gVzh2BIMU96mXfT1S", "6LeJdb4SAAAAAMB6JmsaURxYQxsDQaLb4QtS4gw6", false);
            ReCaptchaResponse response = captcha.checkAnswer(
                    getContext().getRequest().getRemoteAddr(),
                    getContext().getRequest().getParameter("recaptcha_challenge_field"),
                    getContext().getRequest().getParameter("recaptcha_response_field"));

            if (!response.isValid()) {
                getContext().getMessages().add(new SimpleMessage("Неправильная капча. Такие дела."));
                return new RedirectResolution(backUrl);
            }
        }

        if (getCurrentUser() != null) {
            builder.addAuthor(getCurrentUser());
        }


        builder.addCode(code);
        builder.addMessage(message);


        cm.saveOrUpdate(builder.get());

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("added comment ,message="+message);
        }


        return new RedirectResolution(backUrl);
    }
}
